package gui;
import javax.swing.*;

import test.Main;
import database.UserOperations;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;


@SuppressWarnings("serial")
public class loginGUI extends JFrame implements ActionListener
{
	private JPanel p1, p2, sub1, sub2, sub3;
	private JLabel l1, l2, l3, l4;
	private JButton login, forgot, exit, newUser;
	private JTextField f1, f2;
	private UserOperations uo;
	
	public loginGUI() 
	{
		super("Welcome to MilkRun");
		this.setSize(840, 500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		uo=new UserOperations();
		
		//P1
		p1 = new JPanel(new BorderLayout());
		p1.setPreferredSize(new Dimension(300, 500));
		p1.setBackground(Color.red);
		p1.setBorder(BorderFactory.createLineBorder(Color.black));
		
		//p1 sub1
		sub1 = new JPanel();
		sub1.setPreferredSize(new Dimension(200, 100));
		
		//p1 - sub2
		sub2 = new JPanel();
		sub2.setPreferredSize(new Dimension(300, 330));
		
		l1 = new JLabel("Username:");
		l2 = new JLabel("Password:");
		f1 = new JTextField(10);
		f2 = new JTextField(10);
		
		login = new JButton("LOGIN");
		login.addActionListener(this);
		
		forgot = new JButton("Forgot Login Details");
		forgot.addActionListener(this);
		exit = new JButton("Exit");
		exit.addActionListener(this);
		newUser = new JButton("New User");
		newUser.addActionListener(this);
		
		
		sub2.add(Box.createRigidArea(new Dimension(40,0)));
		sub2.add(l1);
		sub2.add(f1);
		sub2.add(Box.createRigidArea(new Dimension(57,0)));
		sub2.add(Box.createRigidArea(new Dimension(30,0)));
		sub2.add(l2);
		sub2.add(f2);
		sub2.add(Box.createRigidArea(new Dimension(45,55)));
		sub2.add(login);
		sub2.add(forgot);
		sub2.add(exit);
		sub2.add(Box.createRigidArea(new Dimension(15,0)));
		sub2.add(newUser);
		
		//p1 sub3
		sub3 = new JPanel();
		sub3.setPreferredSize(new Dimension(300, 100));
		l3 = new JLabel("Copyright MilkRun 2014");
		l4 = new JLabel("Version 1.0");
		sub3.add(Box.createRigidArea(new Dimension(0,98)));
		sub3.add(l3);
		sub3.add(Box.createRigidArea(new Dimension(50,0)));
		sub3.add(l4);
		
		// p1 adding sub panels
		p1.add(sub1, BorderLayout.NORTH);
		p1.add(sub2, BorderLayout.CENTER);
		p1.add(sub3, BorderLayout.SOUTH);
		
		//P2
		p2 = new JPanel();
		p2.setPreferredSize(new Dimension(500, 500));
		p2.setBorder(BorderFactory.createLineBorder(Color.black));
		
		ImageIcon logo = new ImageIcon("src/files/logo3.png");		
		JLabel milkrun = new JLabel(logo);
		p2.add(milkrun);
		
		//Adding Panels
		this.add(p1, BorderLayout.EAST);
		this.add(p2, BorderLayout.WEST);
	}
	
	public static void main(String args[]) 
	{
		loginGUI ls = new loginGUI();
		ls.setVisible(true);
	}
	
	//log in
	public boolean login()
	{
		boolean well=false;
		ArrayList<String> list=uo.userPs();
		int counter=1;
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i).equals(f1.getText()))
			{
				if(list.get(i+1).equals(f2.getText()))
				{
					well=true;
					Main.username=list.get(i);
					Main.userId=counter;
				}
				else
				{
					i++;
					counter++;
				}
			}
			else
			{
				i++;
				counter++;
			}
		}
		return well;
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==login)
		{
			if(login()==true)
			{
				MainScreen ms=new MainScreen();
	            ms.setVisible(true);
	            this.setVisible(false);
			}
			else
			{
				JOptionPane.showMessageDialog(null, "ACCESS DENIED - you did not enter a correct username or password!"
						,"ERROR",JOptionPane.WARNING_MESSAGE);
			}
		}
		if(ae.getSource()==forgot)
		{
			forgotPasswordGUI fpg=new forgotPasswordGUI();
            fpg.setVisible(true);	
		}
		if(ae.getSource()==newUser)
		{
			adminPopupGUI adm=new adminPopupGUI();
			adm.setVisible(true);
		}
		if(ae.getSource()==exit)
		{
			System.exit(0);
		}
	}

}
